{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8140b161",
   "metadata": {},
   "source": [
    "# Lesson 2: Sequential Chats and Customer Onboarding"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9d4d307",
   "metadata": {},
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24b75995-4ee4-4ff0-9c44-3943caae37e7",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": [
    "llm_config={\"model\": \"gpt-3.5-turbo\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20ce6700-8a33-424f-aefe-8852fd1e6d07",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "from autogen import ConversableAgent"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76f979f9",
   "metadata": {},
   "source": [
    "## Creating the needed agents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a527bb1e-dd4e-47b0-a1b7-a9cbcd87cbdb",
   "metadata": {
    "height": 216
   },
   "outputs": [],
   "source": [
    "onboarding_personal_information_agent = ConversableAgent(\n",
    "    name=\"Onboarding Personal Information Agent\",\n",
    "    system_message='''You are a helpful customer onboarding agent,\n",
    "    you are here to help new customers get started with our product.\n",
    "    Your job is to gather customer's name and location.\n",
    "    Do not ask for other information. Return 'TERMINATE' \n",
    "    when you have gathered all the information.''',\n",
    "    llm_config=llm_config,\n",
    "    code_execution_config=False,\n",
    "    human_input_mode=\"NEVER\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51bc9a24-a680-444d-943b-b740bce0189d",
   "metadata": {
    "height": 216
   },
   "outputs": [],
   "source": [
    "onboarding_topic_preference_agent = ConversableAgent(\n",
    "    name=\"Onboarding Topic preference Agent\",\n",
    "    system_message='''You are a helpful customer onboarding agent,\n",
    "    you are here to help new customers get started with our product.\n",
    "    Your job is to gather customer's preferences on news topics.\n",
    "    Do not ask for other information.\n",
    "    Return 'TERMINATE' when you have gathered all the information.''',\n",
    "    llm_config=llm_config,\n",
    "    code_execution_config=False,\n",
    "    human_input_mode=\"NEVER\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6755a7fc-cb17-4d62-a03f-48e260f39010",
   "metadata": {
    "height": 250
   },
   "outputs": [],
   "source": [
    "customer_engagement_agent = ConversableAgent(\n",
    "    name=\"Customer Engagement Agent\",\n",
    "    system_message='''You are a helpful customer service agent\n",
    "    here to provide fun for the customer based on the user's\n",
    "    personal information and topic preferences.\n",
    "    This could include fun facts, jokes, or interesting stories.\n",
    "    Make sure to make it engaging and fun!\n",
    "    Return 'TERMINATE' when you are done.''',\n",
    "    llm_config=llm_config,\n",
    "    code_execution_config=False,\n",
    "    human_input_mode=\"NEVER\",\n",
    "    is_termination_msg=lambda msg: \"terminate\" in msg.get(\"content\").lower(),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64267c0b-f7f2-46e6-ab44-6f7b5fbd9db7",
   "metadata": {
    "height": 148
   },
   "outputs": [],
   "source": [
    "customer_proxy_agent = ConversableAgent(\n",
    "    name=\"customer_proxy_agent\",\n",
    "    llm_config=False,\n",
    "    code_execution_config=False,\n",
    "    human_input_mode=\"ALWAYS\",\n",
    "    is_termination_msg=lambda msg: \"terminate\" in msg.get(\"content\").lower(),\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f240408",
   "metadata": {},
   "source": [
    "## Creating tasks\n",
    "\n",
    "Now, you can craft a series of tasks to facilitate the onboarding process."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b15af1d-7042-4569-a936-7966be203f05",
   "metadata": {
    "height": 607
   },
   "outputs": [],
   "source": [
    "chats = [\n",
    "    {\n",
    "        \"sender\": onboarding_personal_information_agent,\n",
    "        \"recipient\": customer_proxy_agent,\n",
    "        \"message\": \n",
    "            \"Hello, I'm here to help you get started with our product.\"\n",
    "            \"Could you tell me your name and location?\",\n",
    "        \"summary_method\": \"reflection_with_llm\",\n",
    "        \"summary_args\": {\n",
    "            \"summary_prompt\" : \"Return the customer information \"\n",
    "                             \"into as JSON object only: \"\n",
    "                             \"{'name': '', 'location': ''}\",\n",
    "        },\n",
    "        \"max_turns\": 2,\n",
    "        \"clear_history\" : True\n",
    "    },\n",
    "    {\n",
    "        \"sender\": onboarding_topic_preference_agent,\n",
    "        \"recipient\": customer_proxy_agent,\n",
    "        \"message\": \n",
    "                \"Great! Could you tell me what topics you are \"\n",
    "                \"interested in reading about?\",\n",
    "        \"summary_method\": \"reflection_with_llm\",\n",
    "        \"max_turns\": 1,\n",
    "        \"clear_history\" : False\n",
    "    },\n",
    "    {\n",
    "        \"sender\": customer_proxy_agent,\n",
    "        \"recipient\": customer_engagement_agent,\n",
    "        \"message\": \"Let's find something fun to read.\",\n",
    "        \"max_turns\": 1,\n",
    "        \"summary_method\": \"reflection_with_llm\",\n",
    "    },\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "862a066b",
   "metadata": {},
   "source": [
    "## Start the onboarding process"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0fa8f99",
   "metadata": {},
   "source": [
    "**Note**: You might get a slightly different response than what's shown in the video. Feel free to try different inputs, such as name, location, and preferences."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d6d1d4a-0b50-41a5-a1f0-3ff208398bc6",
   "metadata": {
    "height": 63
   },
   "outputs": [],
   "source": [
    "from autogen import initiate_chats\n",
    "\n",
    "chat_results = initiate_chats(chats)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f9e2713",
   "metadata": {},
   "source": [
    "## Print out the summary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e122f8a-1ceb-4635-9672-662114b0552a",
   "metadata": {
    "height": 63
   },
   "outputs": [],
   "source": [
    "for chat_result in chat_results:\n",
    "    print(chat_result.summary)\n",
    "    print(\"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a674c4eb",
   "metadata": {},
   "source": [
    "## Print out the cost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b82a10a-afe5-4ba3-97b4-41c8c14b739f",
   "metadata": {
    "height": 63
   },
   "outputs": [],
   "source": [
    "for chat_result in chat_results:\n",
    "    print(chat_result.cost)\n",
    "    print(\"\\n\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
